package com.ljd.repository.impl;

import com.ljd.entity.Reader;
import com.ljd.repository.ReaderRepository;
import com.ljd.utils.JDBCTools;

import java.sql.*;

public class ReaderRepositoryImpl implements ReaderRepository {
    @Override
    public Reader login(String username, String password) {
        Connection connection=null;
        PreparedStatement ps=null;
        ResultSet rs = null;
        Reader reader=null;
        String sql = "select * from reader where username=? and password=?";
        try {
            connection = JDBCTools.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();
            while(rs.next()) {
                int id=rs.getInt(1);
                String name=rs.getString(4);
                String tel=rs.getString(5);
                String cardid=rs.getString(6);
                String gender=rs.getString(7);
                reader = new Reader(id, username, password, name, tel, cardid, gender);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCTools.release(connection,ps,rs);
        }
        return reader;
    }
}
